#!/usr/bin/env python

'''Isuzu Hana

@author Julot <andy.julot@gmail.com>
@since Monday, July 1, 2013 06:59 PM'''

import argparse
import resource.string.argument as r
import resource.value as v

formatter_class = argparse.RawDescriptionHelpFormatter
parser = argparse.ArgumentParser(description=r.description, epilog=r.epilog,
                                 formatter_class=formatter_class,
                                 prog='hana')
subParser = parser.add_subparsers()

# Telnet Parser
telnetParser = subParser.add_parser('telnet', description=r.telnet.description,
                                    epilog=r.epilog, help=r.telnet.help,
                                    formatter_class=formatter_class)
telnetSubParser = telnetParser.add_subparsers()

# Telnet Server Parser
telnetServerParser = telnetSubParser.add_parser('serve',
        description=r.telnet.server.description, epilog=r.epilog,
        help=r.telnet.server.help, formatter_class=formatter_class)
telnetServerParser.set_defaults(action='telnet.serve')
telnetServerParser.add_argument('-p', '--port', action="store", type=int,
                                default=v.telnet.port, help=r.telnet.port)
telnetServerParser.add_argument('--host', action="store", default=v.host,
                                help=r.host)
telnetServerParser.add_argument('-v', '--verbose', action="store_true",
                                help=r.verbose)
telnetServerParser.add_argument('-f', '--silent', '--quiet',
                                action="store_true", help=r.silent)

# Serial Parser
serialParser = subParser.add_parser('serial', description=r.serial.description,
                                    epilog=r.epilog, help=r.serial.help,
                                    formatter_class=formatter_class)
serialSubParser = serialParser.add_subparsers()

# Serial Server
serialServerParser = serialSubParser.add_parser('serve',
        description=r.serial.server.description, epilog=r.epilog,
        help=r.serial.server.help, formatter_class=formatter_class)
serialServerParser.set_defaults(action='serial.serve')
serialServerParser.add_argument('-p', '--port', action="store",
        default=v.serial.port, help=r.serial.port)
serialServerParser.add_argument('-b', '--baudrate', action="store", type=int,
        default=v.serial.baudrate, help=r.serial.baudrate)
serialServerParser.add_argument('--host', action="store", default=v.host,
                                help=r.host)
serialServerParser.add_argument('--proxyType', action="store", type=int,
                                default=v.proxy.type, help=r.proxy.type,
                                choices=[0, 1, 2, 3, 4])
serialServerParser.add_argument('--proxyHost', action="store",
                                default=v.proxy.host, help=r.proxy.host)
serialServerParser.add_argument('--proxyPort', action="store",
                                default=v.proxy.port, help=r.proxy.port)
serialServerParser.add_argument('--proxyUsername', action="store",
                                default=v.proxy.username, help=r.proxy.username)
serialServerParser.add_argument('--proxyPassword', action="store",
                                default=v.proxy.password, help=r.proxy.password)
serialServerParser.add_argument('-v', '--verbose', action="store_true",
                                help=r.verbose)
serialServerParser.add_argument('-f', '--silent', '--quiet',
                                action="store_true", help=r.silent)

# Serial Radio
serialRadioParser = serialSubParser.add_parser('radio',
        description=r.serial.radio.description, epilog=r.epilog,
        help=r.serial.radio.help, formatter_class=formatter_class)
serialRadioSubParser = serialRadioParser.add_subparsers()

# Serial Radio Sweeper Parser
serialRadioSweeperParser = serialRadioSubParser.add_parser('sweep',
        description=r.serial.radio.sweeper.description, epilog=r.epilog,
        help=r.serial.radio.sweeper.help, formatter_class=formatter_class)
serialRadioSweeperParser.set_defaults(action='serial.radio.sweep')
serialRadioSweeperParser.add_argument('devices', action="store", nargs='+',
        help=r.serial.device, type=int)
serialRadioSweeperParser.add_argument('-p', '--port', action="store",
        default=v.serial.port, help=r.serial.port)
serialRadioSweeperParser.add_argument('-b', '--baudrate', action="store",
        type=int, default=v.serial.baudrate, help=r.serial.baudrate)
serialRadioSweeperParser.add_argument('--timeout', action="store", type=int,
        default=7, help=r.serial.timeout)
serialRadioSweeperParser.add_argument('--host', action="store", default=v.host,
        help=r.host)
serialRadioSweeperParser.add_argument('-v', '--verbose', action="store_true",
        help=r.verbose)
serialRadioSweeperParser.add_argument('-f', '--silent', '--quiet',
        action="store_true", help=r.silent)

# Serial RTU
serialRtuParser = serialSubParser.add_parser('rtu',
        description=r.serial.rtu.description, epilog=r.epilog,
        help=r.serial.rtu.help, formatter_class=formatter_class)
serialRtuSubParser = serialRtuParser.add_subparsers()

# Serial RTU Sweeper Parser
serialRtuSweeperParser = serialRtuSubParser.add_parser('sweep',
        description=r.serial.rtu.sweeper.description, epilog=r.epilog,
        help=r.serial.rtu.sweeper.help, formatter_class=formatter_class)
serialRtuSweeperParser.set_defaults(action='serial.rtu.sweep')
serialRtuSweeperParser.add_argument('devices', action="store", nargs='+',
        help=r.serial.device, type=int)
serialRtuSweeperParser.add_argument('-p', '--port', action="store",
        default=v.serial.port, help=r.serial.port)
serialRtuSweeperParser.add_argument('-b', '--baudrate', action="store",
        type=int, default=v.serial.baudrate, help=r.serial.baudrate)
serialRtuSweeperParser.add_argument('--timeout', action="store", type=int,
        default=7, help=r.serial.timeout)
serialRtuSweeperParser.add_argument('--host', action="store", default=v.host,
        help=r.host)
serialRtuSweeperParser.add_argument('-v', '--verbose', action="store_true",
        help=r.verbose)
serialRtuSweeperParser.add_argument('-f', '--silent', '--quiet',
        action="store_true", help=r.silent)

args = parser.parse_args()
action = args.action

if action == 'telnet.serve':
    import _hana.telnet
    app = _hana.telnet.Server(args)
elif action == 'serial.serve':
    import _hana.serial
    app = _hana.serial.Server(args)
elif action == 'serial.radio.sweep':
    import _hana.serial.radio
    app = _hana.serial.radio.Sweeper(args)
elif action == 'serial.rtu.sweep':
    import _hana.serial.rtu
    app = _hana.serial.rtu.Sweeper(args)

app.run()
del app